package com.yuanfeng.payment.controller;

/**
 * @author Stranger
 * @date 2023/5/18 15:03
 */

import com.alibaba.fastjson.JSON;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.userms.UserInfoDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.exception.DefinedException;
import com.yuanfeng.commoms.exception.GlobalExceptionEnum;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.payment.service.*;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 基础Controller
 */
public class BaseController {

    @Autowired
    protected PayUserBaseService payUserBaseService;

    @Autowired
    protected PayConsumeRecordService payConsumeRecordService;

    @Autowired
    protected PayConsumeTradeService payConsumeTradeService;

    @Autowired
    protected PayUserResourceService payUserResourceService;

    @Autowired
    protected PayCardInfoService payCardInfoService;

    @Autowired
    protected PayAssetDetailsService payAssetDetailsService;

    @Autowired
    protected PayEntranceService payEntranceService;

    @Autowired
    protected ALiPayService aLiPayService;

    @Autowired
    protected WeChatPayServices weChatPayServices;

    @Autowired
    protected PayDistributorEarningsService payDistributorEarningsService;

    @Autowired
    protected PayDistributorStatementService payDistributorStatementService;

    @Autowired
    protected PayConsumeWithdrawService payConsumeWithdrawService;

    @Autowired
    protected  PayAuthorizationAppletInformationService payAuthorizationAppletInformationService;

    @Autowired
    protected PayCardRecordService payCardRecordService;

    @Autowired
    protected PayCardBaseService payCardBaseService;

    @Autowired
    protected PayCardService payCardService;

    @Autowired
    protected PayConfigShopService payConfigShopService;

    @Autowired
    protected PayFxAccountService payFxAccountService;

    @Autowired
    protected PayFxRecordService payFxRecordService;
    /**
     * 根据token得到userInfo
     *
     * @param token
     * @return
     */
    public ResponseResult getUserInfoByToken(String token) {
        //从redis得到token转换userInfo
        boolean isExist = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (!isExist) {
            throw new DefinedException(GlobalExceptionEnum.NO_FORBIDDEN.getErrorCode(), BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        } else {
            String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
            if (dto != null) {
                return ResponseResult.success(dto.getUser());
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());

        }

    }
}
